# Create a sample of CA homes in areas that face non-zero fire risk.
# To be used for both simulation and hedonic analysis
pacman::p_load(raster, tidyverse, sf, fst, data.table)

rm(list = ls())

source("code/globals.R")
sf_use_s2(FALSE) # Explicitly turn off S2

# Load attributes for all homes in 2018 ZTRAX
homes <- read_fst(file.path(WORKING, "ztraxdata", "CAattr2018.fst"),
                  columns = c("ImportParcelID", "BuildingOrImprovementNumber", "County", "PropertyLandUseStndCode",
                              "PropertyFullStreetAddress", "PropertyZip", "PropertyStreetName", "PropertyStreetSuffix",
                              "PropertyAddressLongitude", "PropertyAddressLatitude",
                              "UnformattedAssessorParcelNumber", "FIPS", "sqfeet"), 
                  as.data.table = T)

homes <- homes %>%
  filter(PropertyLandUseStndCode %in% c("RR101", "RR102")) %>%
  filter(BuildingOrImprovementNumber == 1) %>%
  filter(!is.na(PropertyAddressLongitude) & !is.na(PropertyAddressLatitude)) %>%
  st_as_sf(coords = c("PropertyAddressLongitude", "PropertyAddressLatitude"), crs = 4326) %>%
  st_transform(st_crs = 3310) # CA Albers

# Assign wildfire hazard variables 
homes <- assign_wildfire_hazard(homes)

# Separate homes with hazard and no hazard for geocoding
homes_withhazard <- homes %>% filter(wildfire_hazard > 0)
homes_nohazard <- homes %>% filter(wildfire_hazard == 0)

# Save homes as a CSVs for GeoCoding in ArcGIS StreetMap Premium
write_csv(homes_withhazard, file.path(WORKING, "CA-sample-for-geocoding-withhazard.csv.gz")) 
write_csv(homes_nohazard, file.path(WORKING, "CA-sample-for-geocoding-nohazard.csv.gz")) 



